#include <stdio.h>
#include <string.h>
#include <mysql/mysql.h>

int main()
{
    MYSQL conn;
    MYSQL* pconn = mysql_init(&conn);

    if (!pconn) {
        fprintf(stderr, "mysql_init() failed\n");
        return EXIT_FAILURE;
    }

    if (mysql_real_connect(&conn, "localhost", "root", "1234", "Clouddisk", 0, NULL, 0) == NULL) {
        fprintf(stderr, "mysql_real_connect() failed\n");
        mysql_close(pconn);
        return EXIT_FAILURE;
    }

    MYSQL_STMT* stmt = mysql_stmt_init(&conn);
    if (stmt == NULL) {
        fprintf(stderr, "mysql_stmt_init() failed\n");
        mysql_close(pconn);
        return EXIT_FAILURE;
    }

    const char* query = "SELECT * FROM user_register WHERE username LIKE ?";
    if (mysql_stmt_prepare(stmt, query, strlen(query))) {
        fprintf(stderr, "mysql_stmt_prepare() failed: %s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        mysql_close(pconn);
        return EXIT_FAILURE;
    }

    if (mysql_stmt_param_count(stmt) != 1) {
        fprintf(stderr, "Unexpected parameter count.\n");
        mysql_stmt_close(stmt);
        mysql_close(pconn);
        return EXIT_FAILURE;
    }

    char user_name[20] = "zhangteng";
    MYSQL_BIND bind_insert[1];
    memset(bind_insert, 0, sizeof(bind_insert));
    unsigned long len = strlen(user_name);

    bind_insert[0].buffer_type = MYSQL_TYPE_STRING;
    bind_insert[0].buffer = user_name;
    bind_insert[0].buffer_length = len;
    bind_insert[0].is_null = 0;
    bind_insert[0].length = &len;

    if (mysql_stmt_bind_param(stmt, bind_insert)) {
        fprintf(stderr, "mysql_stmt_bind_param() failed: %s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        mysql_close(pconn);
        return EXIT_FAILURE;
    }

    if (mysql_stmt_execute(stmt)) {
        fprintf(stderr, "mysql_stmt_execute() failed: %s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        mysql_close(pconn);
        return EXIT_FAILURE;
    }

    int id = 0;
    char username[20] = {0};
    char cryptpasswd[20] = {0};
    char pwd[100] = {0};

    MYSQL_BIND bind[4];
    memset(bind, 0, sizeof(bind));

    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &id;
    bind[0].buffer_length = sizeof(id);
    bind[0].is_null = 0;

    bind[1].buffer_type = MYSQL_TYPE_STRING;
    bind[1].buffer = username;
    bind[1].buffer_length = sizeof(username);
    bind[1].is_null = 0;

    bind[2].buffer_type = MYSQL_TYPE_STRING;
    bind[2].buffer = cryptpasswd;
    bind[2].buffer_length = sizeof(cryptpasswd);
    bind[2].is_null = 0;

    bind[3].buffer_type = MYSQL_TYPE_STRING;
    bind[3].buffer = pwd;
    bind[3].buffer_length = sizeof(pwd);
    bind[3].is_null = 0;

    if (mysql_stmt_bind_result(stmt, bind)) {
        fprintf(stderr, "mysql_stmt_bind_result() failed: %s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        mysql_close(pconn);
        return EXIT_FAILURE;
    }

    if (mysql_stmt_store_result(stmt)) {
        fprintf(stderr, "mysql_stmt_store_result() failed: %s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        mysql_close(pconn);
        return EXIT_FAILURE;
    }

    while (1) {
        int status = mysql_stmt_fetch(stmt);
        if (status == 1 || status == MYSQL_NO_DATA) {
            break;
        }
        printf("%d\t%s\t%s\t%s\t\n", id, username, cryptpasswd, pwd);
    }

    mysql_stmt_free_result(stmt);
    mysql_stmt_close(stmt);
    mysql_close(pconn);
    return 0;
}

